package com.wondream.myframework.spring.proxy;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

public class JdkProxy {
    public static void main(String[] args) {
        Girl girl = new Girl();
        Human ProxyGirl = (Human)Proxy.newProxyInstance(girl.getClass().getClassLoader(), girl.getClass().getInterfaces(), new InvocationHandler() {
            @Override
            public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
                if (method.getName().equals("eat")) {
                    System.out.println("invoke before");
                    method.invoke(girl, args);
                    System.out.println("invoke after");
                } else {
                    System.out.println("exception");
                }
                return null;
            }
        });
        ProxyGirl.eat();
        ProxyGirl.bath();
    }
}
